var contentMonitor = {
	isCSPATStarted : false,
	
	clickEventHandler : function () {
        if(window.speedtracer && window.speedtracer.isStarted == true) {
            window.speedtracer.clearData();
        }
        backgroundPort.postMessage({type: MSG_TYPE_CLICKED});
	},
	
	addClickEventHandler : function () {
		document.addEventListener("click", contentMonitor.clickEventHandler);
	},
	
	removeClickEventHandler : function () {
		document.removeEventListener("click", contentMonitor.clickEventHandler);
	},
	
	startCSPAT : function () {
		this.isCSPATStarted = true;
		this.addClickEventHandler();
        if(window.speedtracer && window.speedtracer.isStarted == false) {
            window.speedtracer.startMonitoring();
        }

	},
	
	stopCSPAT : function () {
		this.isCSPATStarted = false;
		this.removeClickEventHandler();
        if(window.speedtracer && window.speedtracer.isStarted == true) {
            window.speedtracer.stopMonitoring();
        }
	},

    isSpeedTracerStarted : function() {
        return window.speedtracer.isStarted;
    },

    clearData : function () {
        window.speedtracer.clearData();
        backgroundPort.postMessage({type: MSG_TYPE_DEBUG, data: "Data Cleared!!"});
    },


    getDump : function (callback) {
        window.speedtracer.getDump(callback);
    }
}

// Set up the connection with background. 
var backgroundPort = chrome.runtime.connect({name: PORT_NAME_CONTENT_PAGE});

// This message is sent whenever page is reloaded.
// Then, we should receive  "MSG_TYPE_MONITOR_STAT" as a response to update CSPAT status.
backgroundPort.postMessage({type: MSG_TYPE_INIT});

// Receive messages from background;
backgroundPort.onMessage.addListener(function(request) {
	switch(request.type) {
		case MSG_TYPE_START_CSPAT:
            contentMonitor.clearData();
            contentMonitor.startCSPAT();
			break;
			
		case MSG_TYPE_STOP_CSPAT:
			contentMonitor.stopCSPAT();
			break;

        // Received as the response of message:"MSG_TYPE_INIT".
        case MSG_TYPE_MONITOR_STAT:
            // Update CSPAT status.
			if (request.data) {
				contentMonitor.startCSPAT();
			}
            else {
                contentMonitor.stopCSPAT();
            }
            if (request.isExtensionPageExist) {
                chrome.browserAction.setIcon({"path":"icons/enabled.png"});
            }
			break;
			
		case MSG_TYPE_START_MONITORING:
			//var isStarted = contentMonitor.startSpeedTracer();
			//backgroundPort.postMessage({type: MSG_TYPE_CALLBACK_STARTED, isStarted: isStarted});
			break;
			
		case MSG_TYPE_STOP_MONITORING:
			/*// Retrieve statistics and send them back to background.
			try{
				var innerHTML = document.getElementById("dhtml_statistics").innerHTML;
				var baseTime = window.speedtracer.getBaseTime();
				backgroundPort.postMessage({type:MSG_TYPE_STATS, data:innerHTML, baseTime:baseTime});
			}
			catch(ex) {
				backgroundPort.postMessage({type: MSG_TYPE_LOG, data: ex.message.toString()});
			}
			// Stop monitoring and send dump data to background.
			contentMonitor.stopSpeedTracer(function(dumpData) {
				backgroundPort.postMessage({type: MSG_TYPE_DUMP_DATA, data: dumpData});
			});*/
            //contentMonitor.stopSpeedTracer();
			break;

        case MSG_TYPE_GET_DATA:
            // Retrieve statistics and send them back to background.
            try{
                var innerHTML = document.getElementById("dhtml_statistics").innerHTML;
                backgroundPort.postMessage({type:MSG_TYPE_STATS, data:innerHTML});
            }
            catch(ex) {
				backgroundPort.postMessage({type:MSG_TYPE_STATS, data:0});
                backgroundPort.postMessage({type: MSG_TYPE_ERROR, data: ex.message.toString()});
            }
            // Stop monitoring and send dump data to background.
            contentMonitor.getDump(function(dumpData) {
                backgroundPort.postMessage({type: MSG_TYPE_DUMP_DATA, data: dumpData});
                contentMonitor.clearData();
            });
            break;
			
		case MSG_TYPE_GET_STATS:
			// Retrieve statistics and send them back to background.
			//var innerHTML = document.getElementById("dhtml_statistics").innerHTML;
			//var baseTime = window.speedtracer.getBaseTime();
			//backgroundPort.postMessage({type:MSG_TYPE_STATS, data:innerHTML, baseTime:baseTime});
			break;
			
		default:
			backgroundPort.postMessage({type:MSG_TYPE_LOG, data:"Unrecognized " + request.type});
			break;
	}
});